package com.google.ar.sceneform.samples.hellosceneform

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.MotionEvent
import com.alibaba.fastjson.JSON
import com.google.ar.core.HitResult
import com.google.ar.core.Plane
import com.google.ar.sceneform.AnchorNode
import com.google.ar.sceneform.samples.hellosceneform.hosh.displayinfo.ItemContent
import com.google.ar.sceneform.samples.hellosceneform.hosh.displayinfo.Postion
import com.google.ar.sceneform.samples.hellosceneform.hosh.model.ObjCylinderConfig
import com.google.ar.sceneform.samples.hellosceneform.hosh.model.RotatingNode
import com.google.ar.sceneform.samples.hellosceneform.hosh.model.adapter.ObjCylinderAdapter
import com.google.ar.sceneform.samples.hellosceneform.hosh.model.listener.OnItemClickListener
import java.util.*

class MainActivity : AppCompatActivity() {
    private val TAG = "XXX"
    private var arFragment: CleanArFragment? = null

    private val EXTENTX_CYLINDER = 0.5f // 椭圆主体的宽度
    var config : ObjCylinderConfig = ObjCylinderConfig()
        get() = field
        set(value) {
            if (value == null) {
                field = ObjCylinderConfig();
            } else {
                field = value
            }
        }

    // 初始化显示的数据
    private fun initContent2Show() : MutableMap<String, ItemContent> {
        val content = HashMap<String, ItemContent>()
                val showEle = ItemContent()
                showEle.image = R.drawable.ic_launcher
                showEle.pos = Postion(0, 0)
                content.put(JSON.toJSONString(showEle.pos), showEle)

        return content
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        arFragment = supportFragmentManager.findFragmentById(R.id.ux_fragment) as CleanArFragment?
//        arFragment!!.getArSceneView().getPlaneRenderer().setVisible(false)
        config = ObjCylinderConfig(EXTENTX_CYLINDER)    // 设置最大行半径
        config.objCylinderAdapter = ObjCylinderAdapter(initContent2Show())  // 设置适配器

        config.context = this@MainActivity
        config.ifDebug = false          // 关闭调试模式
        config.loadDebugPointModel(R.drawable.final_point)  // 设置调试模式的中心点的纹理
        config.widthScaleHeight = 1f    // 设置每一行的宽高比
        config.listener = listener      // 添加点击的监听器


        arFragment!!.setOnTapArPlaneListener { hitResult: HitResult, plane: Plane, motionEvent: MotionEvent ->

            // Create the Anchor.
            val anchor = hitResult.createAnchor()
            val anchorNode = AnchorNode(anchor)

            // 为当前节点添加所有面
            val showNode = RotatingNode()
            showNode.worldPosition = anchorNode.worldPosition
            showNode.setParent(arFragment!!.arSceneView.scene)
            config.loadAllFace(showNode)
            // 开始旋转
            showNode.startAnimation()
        }
    }

    var listener = object : OnItemClickListener {
        override fun onItemClicked(row: Int, column: Int) {
            Log.e(TAG, "row=" + row + ", column=" + column)
        }
    }
}
